import tkinter as win
import os
from PIL import Image, ImageTk
from tkinter import filedialog, messagebox
import webbrowser

shifou_save = False
dic = ""

def about():
    about_win = win.Toplevel(root)
    about_win.title("关于 SWPE Word")
    about_win.geometry("500x300")
    about_win.grab_set()
    try:
        current_dir = os.path.dirname(os.path.abspath(__file__))
        icon_filename = "logo_word.png"
        icon_path = os.path.join(current_dir, icon_filename)
        img = Image.open(icon_path)
        img = img.resize((256, 256), Image.Resampling.LANCZOS)
        about_win.about_icon = ImageTk.PhotoImage(img)
        icon_label = win.Label(about_win, image=about_win.about_icon)
        icon_label.pack(side="left", padx=10, pady=10)
    except FileNotFoundError:
        print(f"错误：找不到图标文件 {icon_path}")
        win.Label(about_win, text="图标文件未找到").pack(side="left")
    win.Label(about_win, text="SWPE Word\n版本 4.1.0 beta\n©Snuuyzi.com 2025,保留所有权利").pack(expand=True)
    email_label = win.Label(
        about_win,
        text="反馈：sunzi334481@163.com",
        font=("微软雅黑", 10),
        fg="blue",
        cursor="hand2"
    )
    email_label.pack(anchor="w", pady=2)
    def send_email(event):
        # 格式：mailto:邮箱?subject=主题&body=内容
        mailto_url = "mailto:sunzi334481@163.com?subject=反馈邮件&body=："
        webbrowser.open(mailto_url)


def seetings():
    pass    


def save():
    if shifou_save == False:
        lingcun_save()
    text_content = text_area.get("1.0", win.END).rstrip("\n")
    with open(dic, "w") as f:
        f.write(text_content)
    messagebox.showinfo("保存成功")
    

def lingcun_save():
    save_file = filedialog.asksaveasfile(
        mode="w",
        defaultextension=".txt",
        filetypes=[("文本文件", "*.txt"), ("Python文件", "*.py"), ("所有文件", "*.*")],
        title="另存为"
    )
    if save_file is None:
        messagebox.showinfo("已取消保存")
        return
    text_content = text_area.get("1.0", win.END).rstrip("\n")
    save_file.write(text_content)
    messagebox.showinfo("保存成功")
    save_file.close()
    global dic
    dic = save_file.name
    global shifou_save
    shifou_save = True

root = win.Tk()
root.title("SWPE Word")
root.geometry("1000x600")


all_content = ""
current_dir = os.path.dirname(os.path.abspath(__file__))
icon_path = os.path.join(current_dir, "logo.png")


try:
    img = Image.open(icon_path)
    img = img.resize((32, 32), Image.Resampling.LANCZOS)
    root.icon_img = ImageTk.PhotoImage(img)  
    root.iconphoto(True, root.icon_img)
except Exception as e:
    print(f"图标加载失败: {e}")


caidan = win.Menu(root)
root.config(menu=caidan)


file_menu = win.Menu(caidan, tearoff=0)


file_menu.add_command(label="保存", command=save)
file_menu.add_command(label="另存为", command=lingcun_save)


help_menu = win.Menu(caidan, tearoff=0)
help_menu.add_command(label="关于", command=about)
help_menu.add_command(label="设置", command=seetings)


caidan.add_cascade(label="文件", menu=file_menu)
caidan.add_cascade(label="帮助", menu=help_menu)


text_area = win.Text(
    root,
    wrap=win.WORD,
    font=("楷体", 15)
)
text_area.pack(expand=True, fill="both")

content = text_area.get(1.0, win.END).strip()


root.mainloop()